/////////////////////////////////////////////////////////////////////////////
// 2023 -
//open source under Apache License Version 2.0
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


/**
 * This is a sample program illustrating how to use the Intel ISA-L library.
 * Note it's adapted from erasure_code_test.c test program, but trying to use
 * variable names and styles we're more familiar with already similar to Java
 * coders.
 */

#ifndef _ERASURE_CODER_H_
#define _ERASURE_CODER_H_

namespace Hdfs {
namespace Internal {

#define MMAX 14
#define KMAX 10

typedef struct _IsalCoder {
    int verbose;
    int numParityUnits;
    int numDataUnits;
    int numAllUnits;
} IsalCoder;

typedef struct _IsalEncoder {
    IsalCoder coder;

    unsigned char gftbls[MMAX * KMAX * 32];

    unsigned char encodeMatrix[MMAX * KMAX];
} IsalEncoder;

typedef struct _IsalDecoder {
    IsalCoder coder;

    unsigned char encodeMatrix[MMAX * KMAX];

    // Below are per decode call
    unsigned char gftbls[MMAX * KMAX * 32];
    unsigned int decodeIndex[MMAX];
    unsigned char tmpMatrix[MMAX * KMAX];
    unsigned char invertMatrix[MMAX * KMAX];
    unsigned char decodeMatrix[MMAX * KMAX];
    unsigned char erasureFlags[MMAX];
    int erasedIndexes[MMAX];
    int numErased;
    int numErasedDataUnits;
    unsigned char* realInputs[MMAX];
} IsalDecoder;

void initCoder(IsalCoder * pCoder, int numDataUnits, int numParityUnits);

void initEncoder(IsalEncoder * encoder, int numDataUnits, int numParityUnits, bool verbose);

void initDecoder(IsalDecoder * decoder, int numDataUnits, int numParityUnits, bool verbose);

void clearDecoder(IsalDecoder * decoder);

int innerEncode(IsalEncoder * encoder, unsigned char ** dataUnits,
                unsigned char ** parityUnits, int chunkSize);

int innerDecode(IsalDecoder * decoder, unsigned char ** allUnits,
                int * erasedIndexes, int numErased,
                unsigned char ** recoveredUnits, int chunkSize);

int generateDecodeMatrix(IsalDecoder * pCoder);

}
}

#endif //_ERASURE_CODER_H_